home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10668 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: holland.tessi.com!not-for-mail
  2. From: paulb@holland.tessi.com (Paul Blattner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: friend functions and access to private variables...
  5. Date: 8 Mar 1996 18:00:17 -0800
  6. Organization: Quality Software Engineering, Inc.
  7. Message-ID: <4hqonh$dn7@holland.tessi.com>
  8. References: <Dnyy92.MLL@news.uwindsor.ca>
  9. NNTP-Posting-Host: holland.tessi.com
  10. Keywords: friend
  11.  
  12. In article <Dnyy92.MLL@news.uwindsor.ca>,
  13. Saed Aryan <saed@engn.uwindsor.ca> wrote:
  14. >Hi all,
  15. >
  16. > A friend method of a non friend class has no access to the private member data.
  17. > Apparently the class to which that method belongs must be a friend. The friend
  18. > declaration of that method is then redundant. 
  19.  
  20. Actually, your conclusion is *NOT* accurate.  THe problem is that the 
  21.  
  22.     friend void yMethod(X x); 
  23.  
  24. refers to a *global* function rather than a member of Y. What you needed is 
  25. added below.
  26.  
  27. >
  28. >The code snippet below (compilable) will show what works and what doesn't.
  29. >
  30. >Aryan.
  31. >
  32. >//---- begin code
  33. >class X {
  34. >    friend void yMethod(X x);    // this is for a global (not member) function
  35.  
  36.     friend void Y::yMethod(X x); // adding this makes the call below work
  37.  
  38. >    friend void aFunction(X x);
  39. >    friend class Z;
  40. >    private: int a;
  41. >    };
  42. >
  43.  
  44. . . .
  45.  
  46.  
  47. >class Y { public: void yMethod(X x){x.a=1;}; };
  48. >// OK now: member `a' is a private member of class `X'
  49. >// this is Dariusz's question
  50. >
  51.  
  52. I hope that you find this helpful,
  53.  
  54. Paul Blattner
  55. Quality Software Engineering, Inc.
  56. (503) 538-8256
  57. -- 
  58. Quality Software Engineering, Inc.      The Successful Transition Seminar
  59. PO Box 303                              "Helping companies make a successful
  60. Beaverton  OR  97075                     transition to object technology"
  61. (503)  538-8256                 Training in C++ and Tools.h++
  62.